package com.godyao.mall.seckill.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author godyao
 * @date 2022/4/3
 */
@Configuration
public class RabbitMqConfig {
    /**
     * 交换机名称
     */
    public static final String EXCHANGE_NAME = "seckill_exchanger";

    /**
     * 队列名称
     */
    public static final String QUEUE_NAME = "seckill_queue";

    /**
     * 路由名称
     */
    public static final String ROUTE_KEY = "seckill_key";


    /**
     * 创建一个队列
     */
    @Bean(QUEUE_NAME)
    public Queue seckillQueue() {
        // 开启持久化
        return new Queue(QUEUE_NAME, true);
    }

    @Bean(EXCHANGE_NAME)
    public DirectExchange seckillExchange() {
        return new DirectExchange(EXCHANGE_NAME, true, false);
    }

    @Bean
    public Binding seckillBinding(@Qualifier(QUEUE_NAME) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTE_KEY).noargs();
    }


    ///**
    // * 延迟交换机
    // */
    //public static final String DELAY_EXCHANGE_NAME = "delay.order.exchange";
    ///**
    // * 延迟队列A
    // */
    //public static final String DELAY_QUEUEA_NAME = "delay.order.queuea";
    ///**
    // * 延迟队列B
    // */
    //public static final String DELAY_QUEUEB_NAME = "delay.order.queueb";
    ///**
    // * 延迟队列路由A
    // */
    //public static final String DELAY_QUEUEA_ROUTING_KEY = "delay.order.queuea.routingkey";
    ///**
    // * 延迟队列路由B
    // */
    //public static final String DELAY_QUEUEB_ROUTING_KEY = "delay.order.queueb.routingkey";
    //
    ///**
    // * 死信交换机
    // */
    //public static final String DEAD_LETTER_EXCHANGE = "delay.order.deadletter.exchange";
    ///**
    // * 死信队列A
    // */
    //public static final String DEAD_LETTER_QUEUEA_NAME = "deadletter.order.queuea";
    ///**
    // * 死信队列B
    // */
    //public static final String DEAD_LETTER_QUEUEB_NAME = "deadletter.order.queueb";
    ///**
    // * 死信路由A
    // */
    //public static final String DEAD_LETTER_QUEUEA_ROUTING_KEY = "deadletter.order.15m.routingkey";
    ///**
    // * 死信路由B
    // */
    //public static final String DEAD_LETTER_QUEUEB_ROUTING_KEY = "deadletter.order.30m.routingkey";
    //
    //@Bean
    //public DirectExchange delayExchange() {
    //    return new DirectExchange(DELAY_EXCHANGE_NAME);
    //}
    //
    //@Bean
    //public DirectExchange deadLetterExchange() {
    //    return new DirectExchange(DEAD_LETTER_EXCHANGE);
    //}
    //
    //@Bean
    //public Queue delayQueueA() {
    //    final Map<String, Object> args = new HashMap<>();
    //    // 队列与死信交换机绑定
    //    args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
    //    // 声明当前队列的死信路由key
    //    args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUEA_ROUTING_KEY);
    //    // 声明队列的TTL 为15分钟
    //    args.put("x-message-ttl", TimeUnit.MINUTES.toMillis(15));
    //    return QueueBuilder.durable(DELAY_QUEUEA_NAME).withArguments(args).build();
    //}
    //
    //@Bean
    //public Queue delayQueueB() {
    //    final Map<String, Object> args = new HashMap<>();
    //    // 队列与死信交换机绑定
    //    args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
    //    // 声明当前队列的死信路由key
    //    args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUEB_ROUTING_KEY);
    //    // 声明队列的TTL 为15分钟
    //    args.put("x-message-ttl", TimeUnit.MINUTES.toMillis(30));
    //    return QueueBuilder.durable(DELAY_QUEUEB_NAME).withArguments(args).build();
    //}
    //
    ///***
    //* @author godyao
    //* @description 声明死信队列A 用于接收延迟15分钟的消息
    //* @Param
    //* @return org.springframework.amqp.core.Queue
    //* @date 2022/4/9 16:55
    //*/
    //@Bean
    //public Queue deadLetterQueueA() {
    //    return new Queue(DEAD_LETTER_QUEUEA_NAME);
    //}
    //
    ///***
    //* @author godyao
    //* @description 声明死信队列B 用于接收延迟30分钟的消息
    //* @Param
    //* @return org.springframework.amqp.core.Queue
    //* @date 2022/4/9 16:55
    //*/
    //@Bean
    //public Queue deadLetterQueueB() {
    //    return new Queue(DEAD_LETTER_QUEUEB_NAME);
    //}
    //
    ///***
    //* @author godyao
    //* @description 声明延迟队列A与延迟交换机的绑定关系
    //* @Param
    //* @return org.springframework.amqp.core.Binding
    //* @date 2022/4/9 16:57
    //*/
    //@Bean
    //public Binding delayBindingA(@Qualifier("delayQueueA") Queue queue,
    //                             @Qualifier("delayExchange") DirectExchange exchange) {
    //    return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUEA_ROUTING_KEY);
    //    //return new Binding(DELAY_QUEUEA_NAME, Binding.DestinationType.EXCHANGE, DELAY_EXCHANGE_NAME, DELAY_QUEUEA_ROUTING_KEY, new HashMap<>());
    //}
    //
    ///***
    //* @author godyao
    //* @description 声明延迟队列B与延迟交换机的绑定关系
    //* @Param
    //* @return org.springframework.amqp.core.Binding
    //* @date 2022/4/9 16:57
    //*/
    //@Bean
    //public Binding delayBindingB(@Qualifier("delayQueueB") Queue queue,
    //                             @Qualifier("delayExchange") DirectExchange exchange) {
    //    return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUEA_ROUTING_KEY);
    //}
    //
    ///***
    //* @author godyao
    //* @description 声明死信队列A与死信交换机的绑定关系
    //* @Param queue
    //* @Param exchange
    //* @return org.springframework.amqp.core.Binding
    //* @date 2022/4/9 17:04
    //*/
    //@Bean
    //public Binding deadLetterBindingA(@Qualifier("deadLetterQueueA") Queue queue,
    //                                  @Qualifier("deadLetterExchange") DirectExchange exchange){
    //    return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUEA_ROUTING_KEY);
    //}
    //
    ///***
    //* @author godyao
    //* @description 声明死信队列B与死信交换机的绑定关系
    //* @Param queue
    //* @Param exchange
    //* @return org.springframework.amqp.core.Binding
    //* @date 2022/4/9 17:04
    //*/
    //@Bean
    //public Binding deadLetterBindingB(@Qualifier("deadLetterQueueB") Queue queue,
    //                                  @Qualifier("deadLetterExchange") DirectExchange exchange){
    //    return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUEB_ROUTING_KEY);
    //}
    //
    
    
}
